package com.yinuo.controller;

import com.alibaba.fastjson.JSON;
import com.yinuo.common.*;
import com.yinuo.entity.GlobalContext;
import com.yinuo.entity.JavaCommonClass;
import com.yinuo.entity.PageJson;
import com.yinuo.repository.JavaRepository;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.File;
import java.util.*;

/**
 * Created by gt.ao on 2018/9/25.
 */
@RestController
@RequestMapping("working")
public class WorkingController {
    private static Logger logger = LoggerFactory.getLogger(WorkingController.class);

    @Autowired
    private JavaRepository javaRepository;

    @RequestMapping("openFile")
    public String openFile(String path){
        SystemWaiter.openBrowser("file://"+path);
        return JsonResult.toSuccess();
    }

    @RequestMapping("totalWorkingDocNames")
    public String totalWorkingDocNames(){
        Map<String,Integer> result = new HashMap<String,Integer>(2);
        String rootPath = SystemWaiter.getFilesRootPath();
        List<String> files = FileUtil.getAllFileNamesByPath(rootPath+GlobalContext.WORKING_DOCUMENTS_PATH);
        result.put("total",files.size());
        return JSON.toJSONString(result);
    }

    @RequestMapping("workingDocNames")
    public String workingDocNames(@RequestParam(defaultValue = "0")int curIndex,@RequestParam(defaultValue = "8")int pagesize){
        List<Map<String,String>> result = new ArrayList<Map<String,String>>(20);
        Map<String,String> rmap = null;
        String rootPath = SystemWaiter.getFilesRootPath();
        List<String> files = FileUtil.getAllFileNamesByPath(rootPath+GlobalContext.WORKING_DOCUMENTS_PATH);
        int calcnum = curIndex*pagesize+pagesize;
        calcnum = calcnum>files.size()?files.size():calcnum;
        files = files.subList(curIndex*pagesize,calcnum);
        for(String s:files){
            rmap = new HashMap<>(3);
            rmap.put("imgUrl",getUrl(s));
            rmap.put("fileName",s);
            rmap.put("filePath",SystemWaiter.getFilesRootPath()+"modular/working/workspace/workingDoc/"+s);
            result.add(rmap);
        }
        return JSON.toJSONString(result);
    }

    private String getUrl(String s){
        s = s.substring(s.lastIndexOf("."));
        switch (s){
            case ".doc":
                return "/images/docImg/word.png";
            case ".docx":
                return "/images/docImg/word.png";
            case ".pdf":
                return "/images/docImg/pdf.png";
            case ".txt":
                return "/images/docImg/text.png";
            case ".xls":
                return "/images/docImg/excel.png";
            case ".xlsx":
                return "/images/docImg/excel.png";
            case ".jpg":
                return "/images/docImg/image.png";
            case ".png":
                return "/images/docImg/image.png";
        }
        return "/images/docImg/text.png";
    }

    @RequestMapping("workingDocFolder")
    public void openFolder(String path){
        this.openFile(SystemWaiter.getFilesRootPath()+path);
    }
}
